package com.zaofan.service;

import com.zaofan.bean.Account;
import com.zaofan.bean.Address;
import com.zaofan.bean.OpenBuilder;
import com.zaofan.dao.AddressDao;
import com.zaofan.exception.ServiceException;
import com.zaofan.utils.WebUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AddressService {

	private AddressDao addressDao = new AddressDao();
	private AccountService accountService = new AccountService();
	private OpenBuilderService builderService = new OpenBuilderService();

	public void delete(Address address) throws ServiceException {
		Address target = this.findById(address.getId());
		if(target == null){
			throw new ServiceException("地址信息有误!!");
		}
		if(target.getAccountId() != address.getAccountId()){
			throw new ServiceException("地址信息有误!!");
		}
		addressDao.del(address);
	}

	public int add(Address address) {
		address.setAddTime(new Date());
		addressDao.add(address);
		return address.getId();
	}

	public void update(Address address) {
		addressDao.update(address);
	}

	public List<Address> findAll(Map<String, Object> params) {
		List<Address> data = addressDao.findAll(params);
		if (data == null) {
			data = new ArrayList<>();
		}
		return data;
	}

	public Long count(Map<String, Object> params) {
		return addressDao.count(params);
	}

	public Address findById(Integer id) {
		Map<String, Object> params = new HashMap<>();
		params.put("id", id);
		return addressDao.findOne(params);
	}

	public List<Address> findByAccountId(int accountId) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountId", accountId);
		params.put("order", " by addTime desc");
		return addressDao.findAll(params);
	}

	public int addAddress(Address address) throws ServiceException {
		if (address.getAccountId() == 0) {
			throw new ServiceException("缺少用户信息");
		}
		if (WebUtils.isEmpty(address.getConsignee())) {
			throw new ServiceException("地址收获联系人不能为空!!");
		}
		if (WebUtils.isEmpty(address.getPhone())) {
			throw new ServiceException("地址收获联系人电话不能为空!!");
		}
		if (!WebUtils.isMobileNO(address.getPhone())) {
			throw new ServiceException("地址收获联系人电话格式有误!!");
		}
		if (address.getBuilderId() == 0) {
			throw new ServiceException("大楼地址信息不能为空!!");
		}

		if (WebUtils.isEmpty(address.getFloorNo())) {
			throw new ServiceException("地址楼称信息不能为空!!");
		}
		Account account = accountService.findById(address.getAccountId());
		if(account == null){
			throw new ServiceException("用户信息错误!!");
		}
		address.setMobile(account.getMobile());
		OpenBuilder builder = builderService.findById(address.getBuilderId());
		if(builder == null){
			throw new ServiceException("大楼信息错误!!");
		}
		address.setBuilderName(builder.getBuilderName());
		address.setCityId(builder.getCityId());
		address.setCityName(builder.getCityName());
		address.setAreaId(builder.getAreaId());
		address.setAreaName(builder.getAreaName());
		address.setIsDefault("n");
		address.setAddress(address.getBuilderName() + "-" + address.getDetail());
		return add(address);
	}

	public void updateAddress(Address address) throws ServiceException {
		if (address.getId() == 0) {
			throw new ServiceException("参数缺少id!!");
		}
		if (WebUtils.isEmpty(address.getConsignee())) {
			throw new ServiceException("地址收获联系人不能为空!!");
		}
		if (WebUtils.isEmpty(address.getPhone())) {
			throw new ServiceException("地址收获联系人电话不能为空!!");
		}
		if (!WebUtils.isMobileNO(address.getPhone())) {
			throw new ServiceException("地址收获联系人电话格式有误!!");
		}
		if (address.getBuilderId() == 0) {
			throw new ServiceException("大楼地址信息不能为空!!");
		}
		Account account = accountService.findById(address.getAccountId());
		address.setMobile(account.getMobile());
		OpenBuilder builder = builderService.findById(address.getBuilderId());
		address.setBuilderName(builder.getBuilderName());
		address.setCityId(builder.getCityId());
		address.setCityName(builder.getCityName());
		address.setAreaId(builder.getAreaId());
		address.setAreaName(builder.getAreaName());
		address.setAddress(address.getBuilderName() + "-" + address.getDetail());
		update(address);
	}

	public Address findrecentAddress(int id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountId", id);
		params.put("order", " by addTime desc");
		return addressDao.findAll(params).get(0);
	}

    public Address findDefaultByAccountId(int id) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("accountId", id);
		params.put("isDefault", "y");
		return addressDao.findAll(params).get(0);
    }


}
